﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace DataConvertLib
{
	/// <summary>
	/// Short类型数据转换类
	/// </summary>
	// Token: 0x02000016 RID: 22
	[Description("Short类型数据转换类")]
	public class ShortLib
	{
		/// <summary>
		/// 字节数组中截取转成16位整型
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="start">开始索引</param>
		/// <param name="dataFormat">数据格式</param>
		/// <returns>返回Short结果</returns>
		// Token: 0x060000CA RID: 202 RVA: 0x0000559C File Offset: 0x0000379C
		[Description("字节数组中截取转成16位整型")]
		public static short GetShortFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
		{
			byte[] value2 = ByteArrayLib.Get2BytesFromByteArray(value, start, dataFormat);
			return BitConverter.ToInt16(value2, 0);
		}

		/// <summary>
		/// 将字节数组中截取转成16位整型数组
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="type">数据格式</param>
		/// <returns>返回Short数组</returns>
		// Token: 0x060000CB RID: 203 RVA: 0x000055C0 File Offset: 0x000037C0
		[Description("将字节数组中截取转成16位整型数组")]
		public static short[] GetShortArrayFromByteArray(byte[] value, DataFormat type = DataFormat.ABCD)
		{
			bool flag = value == null;
			if (flag)
			{
				throw new ArgumentNullException("检查数组长度是否为空");
			}
			bool flag2 = value.Length % 2 != 0;
			if (flag2)
			{
				throw new ArgumentNullException("检查数组长度是否为偶数");
			}
			short[] array = new short[value.Length / 2];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = ShortLib.GetShortFromByteArray(value, i * 2, type);
			}
			return array;
		}

		/// <summary>
		/// 将字符串转转成16位整型数组
		/// </summary>
		/// <param name="value">带转换字符串</param>
		/// <param name="spilt">分割符</param>
		/// <returns>返回Short数组</returns>
		// Token: 0x060000CC RID: 204 RVA: 0x00005630 File Offset: 0x00003830
		[Description("将字符串转转成16位整型数组")]
		public static short[] GetShortArrayFromString(string value, string spilt = " ")
		{
			value = value.Trim();
			List<short> list = new List<short>();
			short[] result;
			try
			{
				bool flag = value.Contains(spilt);
				if (flag)
				{
					string[] array = value.Split(new string[]
					{
						spilt
					}, StringSplitOptions.RemoveEmptyEntries);
					foreach (string text in array)
					{
						list.Add(Convert.ToInt16(text.Trim()));
					}
				}
				else
				{
					list.Add(Convert.ToInt16(value.Trim()));
				}
				result = list.ToArray();
			}
			catch (Exception ex)
			{
				throw new ArgumentNullException("数据转换失败：" + ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 设置字节数组某个位
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="offset">某个位</param>
		/// <param name="bitVal">True或者False</param>
		/// <param name="dataFormat">数据格式</param>
		/// <returns>返回short结果</returns>
		// Token: 0x060000CD RID: 205 RVA: 0x000056E8 File Offset: 0x000038E8
		[Description("设置字节数组某个位")]
		public static short SetBitValueFrom2ByteArray(byte[] value, int offset, bool bitVal, DataFormat dataFormat = DataFormat.ABCD)
		{
			bool flag = offset >= 0 && offset <= 7;
			if (flag)
			{
				value[1] = ByteLib.SetbitValue(value[1], offset, bitVal);
			}
			else
			{
				value[0] = ByteLib.SetbitValue(value[0], offset - 8, bitVal);
			}
			return ShortLib.GetShortFromByteArray(value, 0, dataFormat);
		}

		/// <summary>
		/// 设置16位整型某个位
		/// </summary>
		/// <param name="value">Short数据</param>
		/// <param name="offset">某个位</param>
		/// <param name="bitVal">True或者False</param>
		/// <param name="dataFormat">数据格式</param>
		/// <returns>返回Short结果</returns>
		// Token: 0x060000CE RID: 206 RVA: 0x00005738 File Offset: 0x00003938
		[Description("设置16位整型某个位")]
		public static short SetBitValueFromShort(short value, int offset, bool bitVal, DataFormat dataFormat = DataFormat.ABCD)
		{
			byte[] byteArrayFromShort = ByteArrayLib.GetByteArrayFromShort(value, dataFormat);
			return ShortLib.SetBitValueFrom2ByteArray(byteArrayFromShort, offset, bitVal, dataFormat);
		}

		/// <summary>
		/// 通过布尔长度取整数
		/// </summary>
		/// <param name="boolLength">布尔长度</param>
		/// <returns>整数</returns>
		// Token: 0x060000CF RID: 207 RVA: 0x0000575C File Offset: 0x0000395C
		[Description("通过布尔长度取整数")]
		public static short GetByteLengthFromBoolLength(int boolLength)
		{
			return (boolLength % 8 == 0) ? ((short)(boolLength / 8)) : ((short)(boolLength / 8 + 1));
		}
	}
}
